<?php
// +----------------------------------------------------------------------
// | BWCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.chajiandaquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Baiwapak <baiwapak@qq.com>
// +----------------------------------------------------------------------

defined('THINK_PATH') or exit();
/**
 * BW标签库解析类
 * @author    baiwapak <baiwapak@qq.com>
 */
class TagLibPth extends TagLib {
    // 标签定义
    protected $tags   =  array(
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
		'article'       => array('attr'=>'id, cateid, order, limit, son'),
		'friendlinks'       => array('attr'=>'id, order, limit'),
		'websites'       => array('attr'=>'id, cateid, order, limit, type'),
		'comment'       => array('attr'=>'id, target_id, types, page, limit, showpage'),
		'info' 	 	 => array('attr'=>'id,substr', 'close'=>0),
		'about' 	 	 => array('attr'=>'id'),
        );
		
	/**
     * article标签解析 循环输出数据集
     * 格式：
     * <article cateid="1" limit="10">
     * {$vo.name}
     * </article>
     * @access public
     * @param string $attr 标签属性
     * @param string $content  标签内容
     * @return string|void
     */
    public function _article($attr,$content) {
        static $_iterateParseCache = array();
        //如果已经解析过，则直接返回变量值
        $cacheIterateId = md5($attr.$content);
        if(isset($_iterateParseCache[$cacheIterateId])){
            return $_iterateParseCache[$cacheIterateId];
		}
			
        $tag       =    $this->parseXmlAttr($attr,'article');
		$son       =    empty($tag['son']) ? true : (bool)($tag['son']); //显示所有下级分类,默认是
		
		$parseStr  = '<?php $map = array(); $map["status"]=1; ?>';
	    $parseStr .= '<?php $cateObject = D("NewsCate"); ?>';
	  
		  if(isset($tag['cateid']) ){
			if((intval($tag['cateid']) > 0) && strlen(intval($tag['cateid'])) == strlen($tag['cateid'])){
				$cateid    =    intval($tag['cateid']);
				if($son && $cateid > 0){
					$parseStr  .= '<?php $cateIdList = $cateObject->getCatePathToIdText('.$cateid.'); $map["cate_id"]= array("IN", $cateIdList);  ?>';	
				}else if($cateid > 0){
					$parseStr  .= '<?php $map["cate_id"]='.$cateid.';  ?>';		
				}
				
			}else{
				$cateid    =    $this->autoBuildVar($tag['cateid']);
				$parseStr  .= '<?php $map["cate_id"]='.$cateid.';  ?>';
				
			}
		  }
		
		$limit     =    empty($tag['limit']) ? 10 : trim($tag['limit']);
		
		$order     =    empty($tag['order']) ? 'id DESC' : $tag['order'];
		$id        =    empty($tag['id']) ? 'vo' : $tag['id'];
	  
	  
		$parseStr  .= '<?php $_result = D("News")->limit('.$limit.')->where($map)->order("'.$order.'")->select(); foreach($_result as $key=>$'.$id.'): ';
		$parseStr .= '$'.$id.'[cate_name] = $cateTextArray[$'.$id.'[cate_id]]; $'.$id.'[url] = setViewUrl("article", $'.$id.');
		?>';
		
		$parseStr .= $this->tpl->parse($content); 
		$parseStr .= '<?php endforeach; ?>';
		//p($parseStr );
		$_iterateParseCache[$cacheIterateId] = $parseStr;

        if(!empty($parseStr)) {
            return $parseStr;
        }
        return ;
    }	
	
	
	/**
     * websites标签解析 循环输出数据集
     * 格式：
     * <websites cateid="1" limit="10">
     * {$vo.name}
     * </websites>
     * @access public
     * @param string $attr 标签属性
     * @param string $content  标签内容
     * @return string|void
     *///'websites'       => array('attr'=>'id, cateid, order, limit'),
    public function _websites($attr,$content) {
        static $_iterateParseCache = array();
        //如果已经解析过，则直接返回变量值
        $cacheIterateId = md5($attr.$content);
        if(isset($_iterateParseCache[$cacheIterateId])){
            return $_iterateParseCache[$cacheIterateId];
		}
		
        $tag       = $this->parseXmlAttr($attr,'websites');
		$son       = empty($tag['son']) ? true : (bool)($tag['son']); //显示所有下级分类,默认是
		
		$parseStr  = '<?php $map = array(); $map["status"]=1; ?>';
	    $parseStr .= '<?php $cateObject = D("WebsitesCate"); ?>';
	  
		  if(isset($tag['cateid']) ){
			if((intval($tag['cateid']) > 0) && strlen(intval($tag['cateid'])) == strlen($tag['cateid'])){
				$cateid    =    intval($tag['cateid']);
				if($son && $cateid > 0){
					$parseStr  .= '<?php $cateIdList = $cateObject->getCatePathToIdText('.$cateid.'); $map["cate_id"]= array("IN", $cateIdList);  ?>';	
				}else if($cateid > 0){
					$parseStr  .= '<?php $map["cate_id"]='.$cateid.';  ?>';		
				}
				
			}else{
				$cateid    =    $this->autoBuildVar($tag['cateid']);
				$parseStr  .= '<?php $map["cate_id"]='.$cateid.';  ?>';
				
			}
		  }
		
		
        $type      = empty($tag['type']) ? '' : trim($tag['type']);
		$limit     = empty($tag['limit']) ? 10 : trim($tag['limit']);
		$order     = empty($tag['order']) ? 'id DESC' : $tag['order'];
		$id        = empty($tag['id']) ? 'vo' : $tag['id'];
		
		
		if(!empty($type)){
			switch($type){
				case 'is_best':
				case 'best':
					$parseStr  .= '<?php $map["is_best"]= 1;  ?>';		
				break;	
				case 'is_pay':
				case 'pay':
					$parseStr  .= '<?php $map["is_pay"]= 1;  ?>';		
				break;	
				case 'is_top':
				case 'top':
					$parseStr  .= '<?php $map["is_top"]= 1;  ?>';		
				break;	
			}
		}
		
		$parseStr  .= '<?php $_result=D("WebsitesView")->limit('.$limit.')->where($map)->order("'.$order.'")->select(); $cateTextArray = $cateObject->getAllCateTextArray(); foreach($_result as $key=>$'.$id.'): ';
		$parseStr .= '$'.$id.'[cate_name] = $cateTextArray[$'.$id.'[cate_id]]; $'.$id.'[url] = setViewUrl("site", $'.$id.');
		?>';
		$parseStr .= $this->tpl->parse($content); 
		$parseStr .= '<?php endforeach; ?>';
		
		$_iterateParseCache[$cacheIterateId] = $parseStr;

        if(!empty($parseStr)) {
            return $parseStr;
        }
        return ;
    }	
	
	
	/**
     * article标签解析 循环输出数据集
     * 格式：
     * <article cateid="1" limit="10">
     * {$vo.name}
     * </article>
     * @access public
     * @param string $attr 标签属性
     * @param string $content  标签内容
     * @return string|void
     */
    public function _friendlinks($attr,$content) {
        static $_iterateParseCache = array();
        //如果已经解析过，则直接返回变量值
        $cacheIterateId = md5($attr.$content);
        if(isset($_iterateParseCache[$cacheIterateId])){
            return $_iterateParseCache[$cacheIterateId];
		}
			
        $tag       =    $this->parseXmlAttr($attr,'friendlinks');
		$limit     =    empty($tag['limit']) ? 10 : trim($tag['limit']);
		$order     =    empty($tag['order']) ? 'id DESC' : $tag['order'];
		$id        =    empty($tag['id']) ? 'vo' : $tag['id'];
	   
	    $object = D('Friendlinks');
		
		$result = $object->limit($limit)->where('status = 1 ')->order($order)->select();
		
		$list = serialize($result);//p($list);
	   	$parseStr  = '<?php $_results = \''.$list.'\'; $_result = unserialize($_results); foreach($_result as $key=>$'.$id.'): ?>';
		$parseStr .= $this->tpl->parse($content); 
		$parseStr .= '<?php endforeach; ?>';
		//p($parseStr );
		$_iterateParseCache[$cacheIterateId] = $parseStr;

        if(!empty($parseStr)) {
            return $parseStr;
        }
        return ;
    }
		
	/**
     * _comment标签解析 评论
     * 格式：
     * <article cateid="1" limit="10">
     * {$vo.name}
     * </article>
     * @access public
     * @param string $attr 标签属性
     * @param string $content  标签内容
     * @return string|void
     */
    public function _comment($attr,$content) {
        static $_iterateParseCache = array();
        //如果已经解析过，则直接返回变量值
        $cacheIterateId = md5($attr.$content);
        if(isset($_iterateParseCache[$cacheIterateId])){
            return $_iterateParseCache[$cacheIterateId];
		}
		
		//'id, target_id, types, page, limit'
		//$modelViewObject   = D('CommentView');
        $tag               = $this->parseXmlAttr($attr,'comment');
		$map = array();
		$map['types']      = empty($tag['types']) ? '' : trim($tag['types']);
        $map['target_id']  = $this->autoBuildVar($tag['target_id']);
		
		$pageSize     	   = empty($tag['limit']) ? 10 : intval($tag['limit']);
		$thisPage     	   = $this->autoBuildVar($tag['page']);
		$showPage     	   = intval($tag['showpage']) > 0 ? true : false;
		$id            	   = empty($tag['id']) ? 'vo' : $tag['id'];
		 
		$parseStr  = '<?php $map = array(); $map["status"]=1;$map["target_id"]='.$map['target_id'].';$map["types"]="'.$map['types'].'";  ?>';
		if($showPage){
			$parseStr  .= '<?php $'.$id.'=D("CommentView")->buildPage($map, '.$thisPage.', '.$pageSize.'); if($'.$id.'[count] > 0): ?>';
			$parseStr .= $this->tpl->parse($content); 
			$parseStr .= '<?php endif; ?>';
		 }else{
			$parseStr  .= '<?php $_result=D("CommentView")->getPageList($map, '.$thisPage.', '.$pageSize.'); foreach($_result as $key=>$'.$id.'): ?>';
			$parseStr .= $this->tpl->parse($content); 
			$parseStr .= '<?php endforeach; ?>';
		 }
		$_iterateParseCache[$cacheIterateId] = $parseStr;

        if(!empty($parseStr)) {
            return $parseStr;
        }
        return ;
    }
	
		/**
     * about标签解析 循环输出数据集
     * 格式：
     * <about >
     * {$vo.name}
     * </about>
     * @access public
     * @param string $attr 标签属性
     * @param string $content  标签内容
     * @return string|void
     */
    public function _about($attr,$content) {
        static $_iterateParseCache = array();
        //如果已经解析过，则直接返回变量值
        $cacheIterateId = md5($attr.$content);
        if(isset($_iterateParseCache[$cacheIterateId])){
            return $_iterateParseCache[$cacheIterateId];
		}
			
        $tag       =    $this->parseXmlAttr($attr,'about');
		$id        =    empty($tag['id']) ? 'vo' : $tag['id'];
	   
	    $object = D('About');
		
		$result = $object->getList();
		
		$list = serialize($result);//p($list);
	   	$parseStr  = '<?php $_results = \''.$list.'\'; $_result = unserialize($_results); foreach($_result as $key=>$'.$id.'): ?>';
		$parseStr .= $this->tpl->parse($content); 
		$parseStr .= '<?php endforeach; ?>';
		//p($parseStr );
		$_iterateParseCache[$cacheIterateId] = $parseStr;

        if(!empty($parseStr)) {
            return $parseStr;
        }
        return ;
    }
	
	/**
     * info标签解析 输出单篇栏目信息
     * 格式：
     * <info id="1" substr='100' />
     * @access public
     * @param string $attr 标签属性
     * @param string $content  标签内容
     * @return string|void
     */
    public function _info($attr,$content) {
        static $_iterateParseCache = array();
        //如果已经解析过，则直接返回变量值
        $cacheIterateId = md5($attr.$content);
        if(isset($_iterateParseCache[$cacheIterateId])){
            return $_iterateParseCache[$cacheIterateId];
		}
			
        $tag      =    $this->parseXmlAttr($attr,'info');
		$id       =    $tag['id'];
		$substr   =   !empty( $tag['substr']) ? $tag['substr'] : 300;
        
		if(empty($id)){return ;}
		
		$result = "M('About')->field('content')->find($id)";
	   	
	   	$parseStr  = '<?php $_result = '.$result.'; ?>';
		//$parseStr .= $this->tpl->parse($content); 
		$parseStr .= '<?php echo mbsubstr(strip_tags(htmlspecialchars_decode($_result[\'content\'])), 0, '.$substr.'); ?>';
		
		$_iterateParseCache[$cacheIterateId] = $parseStr;

        if(!empty($parseStr)) {
            return $parseStr;
        }
        return ;
    }
}